package com.sxj.corejava.code12_线程;

/**
 * @author 石小俊
 */
public class Test03_阻塞相关方法 {

    public static void main(String[] args) {
        MyRunnable mr = new MyRunnable();
        Thread t1 = new Thread(mr, "线程1");
        t1.start();
//        t1.interrupt();
        for (int i = 0; i < 100; i++) {
            try {
                if (i == 10) {
//                    Thread.sleep(3000);
                    // t1.interrupt(); // 打断休眠
                    t1.join();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "正在打印:" + i);
        }
        // 在此处打断休眠是无效的,因为此时的t1已经醒了
//        t1.interrupt();
    }

}

class MyRunnable implements Runnable {

    @Override
    public void run() {
        for (int i = 0; i < 100; i++) {
            try {
                if (i == 10) {
                    Thread.sleep(3000);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println(Thread.currentThread().getName() + "正在打印:" + i);
        }
    }
}
